package org.wdcode.back.aop;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;

import org.wdcode.back.exception.RightsException;
import org.wdcode.back.helper.LogsHelper;
import org.wdcode.back.helper.OperateHelper;
import org.wdcode.back.helper.UserHelper;
import org.wdcode.back.po.Department;
import org.wdcode.back.po.Logs;
import org.wdcode.back.po.Operate;
import org.wdcode.back.po.User;
import org.wdcode.back.service.LogsService;
import org.wdcode.base.aop.Advice;
import org.wdcode.base.bean.EntityBean;
import org.wdcode.base.util.Struts2Util;
import org.wdcode.common.constants.StringConstants;
import org.wdcode.common.log.WdLogs;
import org.wdcode.common.util.CommonUtil;
import org.wdcode.common.util.DateUtil;
import org.wdcode.core.concurrent.ExecutorFactory;

/**
 * AOP通知 用于拦截Action
 * @author WD
 * @since JDK6
 * @version 1.0 2009-09-24
 */
public final class BasicAdvice implements Advice {
	// 日志业务
	private LogsService			logsService;
	// 删除多个数据标识
	private final static String	DELS	= "dels";
	// 实体标识
	private final static String	ENTITY	= "entity";
	// ID数组标识
	private final static String	IDS		= "ids";

	/**
	 * 前置通知方法 验证权限
	 * @param point aop切点信息
	 */
	public void before(JoinPoint point) {
		try {
			// 判断是否有权限
			if (!OperateHelper.getHelper().isRights()) {
				throw new RightsException();
			}
		} catch (RuntimeException e) {
			WdLogs.error(e);
		}
	}

	/**
	 * 后置通知方法 记录日志
	 * @param point aop切点信息
	 */
	public void afterReturning(JoinPoint point) {
		try {
			// 获得request
			HttpServletRequest request = Struts2Util.getRequest();
			// 获得提交的连接
			final String link = request.getServletPath();
			// 获得操作实体
			final EntityBean bean = (EntityBean) request.getAttribute(ENTITY);
			// 获得删除的IDS
			final Object ids = request.getAttribute(IDS);
			// 获得用户实体
			final User user = UserHelper.getHelper().getUser();

			// 开启线程添加操作
			ExecutorFactory.getFactory().getInstance().execute(new Runnable() {
				@Override
				public void run() {
					// // 根据link获得操作
					Operate operate = OperateHelper.getHelper().getByLink(link);
					// 获得部门
					Department depa = user.getDepartment();
					// 添加日志
					Logs logs = LogsHelper.getHelper().newInstance();
					logs.setCompanyId(depa.getCompanyId());
					logs.setDepartmentId(depa.getId());
					logs.setUserId(user.getId());
					logs.setRoleId(user.getRoleId());
					logs.setOperateId(operate.getId());
					logs.setCreateDate(DateUtil.getTime());
					// 判断操作
					if (link.indexOf(DELS) == -1) {
						// 判断实体不为空
						if (!CommonUtil.isEmpty(bean)) {
							logs.setContent(bean.getId() + StringConstants.MIDLINE + bean.getName());
						}
					} else {
						// 删除多个数据
						logs.setContent(Arrays.toString((int[]) ids));
					}
					// 记录日志
					logsService.insert(logs);
				}
			});
		} catch (RuntimeException e) {
			WdLogs.error(e);
		}
	}

	/**
	 * 拦截Throwable异常 记录异常
	 * @param point aop切点信息
	 */
	public void afterThrowing(JoinPoint point, Throwable t) {
		WdLogs.error(point.getTarget(), t);
	}

	/**
	 * 获得日志业务
	 * @return 日志业务
	 */
	public LogsService getLogsService() {
		return logsService;
	}

	/**
	 * 设置日志业务
	 * @param logsService 日志业务
	 */
	public void setLogsService(LogsService logsService) {
		this.logsService = logsService;
	}
}
